package com.train.order.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(path = {"/order"})
public class OrderController {
    @Value("spring.application.name")
    private String name;

    @Value(value = "#{T(java.lang.Integer).parseInt('${server.port:0}')}")
    private Integer port;
    @Resource
    private DiscoveryClient discoveryClient;


    @RequestMapping(method = {RequestMethod.GET})
    public String hi(){
        try {
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            return "this is "+ name +" port:"+ port;
        }
//        List<String> services = discoveryClient.getServices();
//        services.forEach(System.out::println);
//
//        List<ServiceInstance> service_ports = discoveryClient.getInstances(services.get(0));
//        ServiceInstance serviceInstance = service_ports.get(0);
//        System.out.println(serviceInstance.getHost()+" "+serviceInstance.getPort());
    }
}
